diff options
author | Mike Buland <eichlan@xagasoft.com> | 2011-12-30 15:44:06 -0700 |
---|---|---|
committer | Mike Buland <eichlan@xagasoft.com> | 2011-12-30 15:44:06 -0700 |
commit | ba7897ebadbc03d99200fda03a574a57b650e429 (patch) | |
tree | b13548a7c33e52d9f09e1563b1b284a041edb216 | |
parent | f2ee67558acbe3c418a7558587b56158d593d88d (diff) | |
download | stage-ba7897ebadbc03d99200fda03a574a57b650e429.tar.gz stage-ba7897ebadbc03d99200fda03a574a57b650e429.tar.bz2 stage-ba7897ebadbc03d99200fda03a574a57b650e429.tar.xz stage-ba7897ebadbc03d99200fda03a574a57b650e429.zip |
Dictionaries can be created, in operator works.
-rw-r--r-- | src/gamestate.cpp | 22 | ||||
-rw-r--r-- | src/parser.y | 10 | ||||
-rw-r--r-- | src/variable.cpp | 17 | ||||
-rw-r--r-- | src/variable.h | 5 | ||||
-rw-r--r-- | test.stage | 3 |
5 files changed, 51 insertions, 6 deletions
diff --git a/src/gamestate.cpp b/src/gamestate.cpp index d45d53c..e8df070 100644 --- a/src/gamestate.cpp +++ b/src/gamestate.cpp | |||
@@ -408,6 +408,20 @@ void GameState::parse( const AstBranch::NodeList &lCode ) | |||
408 | break; | 408 | break; |
409 | 409 | ||
410 | case AstNode::tIn: | 410 | case AstNode::tIn: |
411 | { | ||
412 | Variable v = popDeref(); | ||
413 | Variable x = popDeref(); | ||
414 | if( v.getType() == Variable::tDictionary ) | ||
415 | { | ||
416 | push( Variable( v.has( x ) ) ); | ||
417 | } | ||
418 | else | ||
419 | { | ||
420 | throw Bu::ExceptionBase("Invalid type for operator in"); | ||
421 | } | ||
422 | } | ||
423 | break; | ||
424 | |||
411 | case AstNode::tGoto: | 425 | case AstNode::tGoto: |
412 | { | 426 | { |
413 | Variable x = popDeref(); | 427 | Variable x = popDeref(); |
@@ -439,6 +453,14 @@ void GameState::parse( const AstBranch::NodeList &lCode ) | |||
439 | } | 453 | } |
440 | break; | 454 | break; |
441 | 455 | ||
456 | case AstNode::tInsert: | ||
457 | { | ||
458 | Variable v = popDeref(); | ||
459 | Variable k = popDeref(); | ||
460 | lStack.peek().insert( k, v ); | ||
461 | } | ||
462 | break; | ||
463 | |||
442 | // tLeafLiteral | 464 | // tLeafLiteral |
443 | case AstNode::tVarName: | 465 | case AstNode::tVarName: |
444 | case AstNode::tLiteral: | 466 | case AstNode::tLiteral: |
diff --git a/src/parser.y b/src/parser.y index f01e0e4..707d85d 100644 --- a/src/parser.y +++ b/src/parser.y | |||
@@ -82,9 +82,9 @@ void yyerror( YYLTYPE *llocp, yyscan_t yyscanner, GameBuilder &, const char *err | |||
82 | 82 | ||
83 | %left NOT | 83 | %left NOT |
84 | %right '=' tokPlusAssign tokMinusAssign tokTimesAssign tokDivideAssign | 84 | %right '=' tokPlusAssign tokMinusAssign tokTimesAssign tokDivideAssign |
85 | %left tokIn tokAnd tokOr | ||
85 | %left '-' '+' | 86 | %left '-' '+' |
86 | %left '*' '/' | 87 | %left '*' '/' |
87 | %left tokIn tokAnd tokOr | ||
88 | %right '<' '>' tokLtEq tokGtEq tokCmp | 88 | %right '<' '>' tokLtEq tokGtEq tokCmp |
89 | %left '(' ')' '[' ']' | 89 | %left '(' ')' '[' ']' |
90 | %left NEG | 90 | %left NEG |
@@ -229,8 +229,8 @@ expr: literal | |||
229 | | expr '[' expr ']' | 229 | | expr '[' expr ']' |
230 | | '[' ']' { bld.addLiteral( Variable( Variable::tList ) ); } | 230 | | '[' ']' { bld.addLiteral( Variable( Variable::tList ) ); } |
231 | | '[' { bld.addLiteral( Variable( Variable::tList ) ); } listValues ']' | 231 | | '[' { bld.addLiteral( Variable( Variable::tList ) ); } listValues ']' |
232 | | '{' '}' | 232 | | '{' '}' { bld.addLiteral( Variable( Variable::tDictionary ) ); } |
233 | | '{' dictValues '}' | 233 | | '{' { bld.addLiteral( Variable( Variable::tDictionary ) ); } dictValues '}' |
234 | | tokNot expr %prec NOT { bld.addNode( AstNode::tNot ); } | 234 | | tokNot expr %prec NOT { bld.addNode( AstNode::tNot ); } |
235 | | '-' expr %prec NEG { bld.addNode( AstNode::tNegate ); } | 235 | | '-' expr %prec NEG { bld.addNode( AstNode::tNegate ); } |
236 | ; | 236 | ; |
@@ -247,8 +247,8 @@ listValues: expr { bld.addNode( AstNode::tAppend ); } | |||
247 | | listValues ',' expr { bld.addNode( AstNode::tAppend ); } | 247 | | listValues ',' expr { bld.addNode( AstNode::tAppend ); } |
248 | ; | 248 | ; |
249 | 249 | ||
250 | dictValues: expr ':' expr | 250 | dictValues: expr ':' expr { bld.addNode( AstNode::tInsert ); } |
251 | | dictValues ',' expr ':' expr | 251 | | dictValues ',' expr ':' expr { bld.addNode( AstNode::tInsert ); } |
252 | ; | 252 | ; |
253 | 253 | ||
254 | commandDecl: tokCommand ':' tokString { bld.beginCommand( *$3 ); } | 254 | commandDecl: tokCommand ':' tokString { bld.beginCommand( *$3 ); } |
diff --git a/src/variable.cpp b/src/variable.cpp index db7726c..1e5cc9b 100644 --- a/src/variable.cpp +++ b/src/variable.cpp | |||
@@ -210,6 +210,23 @@ Variable Variable::to( Type e ) const | |||
210 | eType, e ); | 210 | eType, e ); |
211 | } | 211 | } |
212 | 212 | ||
213 | void Variable::insert( const Variable &vKey, const Variable &vValue ) | ||
214 | { | ||
215 | if( eType != tDictionary ) | ||
216 | throw Bu::ExceptionBase("Insert on non-dictionary."); | ||
217 | hValue->insert( vKey, vValue ); | ||
218 | } | ||
219 | |||
220 | bool Variable::has( const Variable &vKey ) | ||
221 | { | ||
222 | if( eType == tDictionary ) | ||
223 | return hValue->has( vKey ); | ||
224 | // else if( eType == tList ) | ||
225 | // return lValue->contains( vKey ); | ||
226 | else | ||
227 | throw Bu::ExceptionBase("Insert on non-dictionary."); | ||
228 | } | ||
229 | |||
213 | Variable &Variable::operator=( const Variable &rhs ) | 230 | Variable &Variable::operator=( const Variable &rhs ) |
214 | { | 231 | { |
215 | deinitType(); | 232 | deinitType(); |
diff --git a/src/variable.h b/src/variable.h index 7f482a3..97308c7 100644 --- a/src/variable.h +++ b/src/variable.h | |||
@@ -31,6 +31,7 @@ public: | |||
31 | tString, | 31 | tString, |
32 | tSituation, | 32 | tSituation, |
33 | tVariable, | 33 | tVariable, |
34 | tVarPtr, | ||
34 | tList, | 35 | tList, |
35 | tDictionary | 36 | tDictionary |
36 | }; | 37 | }; |
@@ -58,6 +59,9 @@ public: | |||
58 | 59 | ||
59 | Variable to( Type e ) const; | 60 | Variable to( Type e ) const; |
60 | 61 | ||
62 | void insert( const Variable &vKey, const Variable &vValue ); | ||
63 | bool has( const Variable &vKey ); | ||
64 | |||
61 | Variable &operator=( const Variable &rhs ); | 65 | Variable &operator=( const Variable &rhs ); |
62 | Variable &operator+=( const Variable &rhs ); | 66 | Variable &operator+=( const Variable &rhs ); |
63 | Variable &operator-=( const Variable &rhs ); | 67 | Variable &operator-=( const Variable &rhs ); |
@@ -94,6 +98,7 @@ private: | |||
94 | VList *lValue; | 98 | VList *lValue; |
95 | VHash *hValue; | 99 | VHash *hValue; |
96 | VariableRef *rValue; | 100 | VariableRef *rValue; |
101 | Variable *pValue; | ||
97 | }; | 102 | }; |
98 | }; | 103 | }; |
99 | 104 | ||
@@ -61,8 +61,9 @@ situation <<start>> | |||
61 | } | 61 | } |
62 | setup | 62 | setup |
63 | { | 63 | { |
64 | stuff = [5, 10, "bob"]; | 64 | stuff = {"things": 55, 112: "Bob"}; |
65 | display( stuff ); | 65 | display( stuff ); |
66 | display( 113 - 1 in stuff ); | ||
66 | exit(); | 67 | exit(); |
67 | } | 68 | } |
68 | 69 | ||